package com.ls.enums;

import com.ls.entity.SysUser;
import com.ls.exception.SysException;

/**
 * <p>
 * 用户类型枚举类
 * </p>
 *
 * @author pengyu
 * @since 2020-05-25
 */
public enum RoleEnum {
    /**
     * 超级管理员
     */
    ADMIN(1,"admin"),

    /**
     * 平台客服
     */
    STAFF(2,"staff"),

    /**
     * 会员
     */
    MEMBER(3,"member"),

    /**
     * 非会员
     */
    COMMON(4,"common");

    public long roleType;
    public String roleName;

    RoleEnum(long roleType, String roleName) {
        this.roleType = roleType;
        this.roleName = roleName;
    }

    public static RoleEnum getRoleEnum(long type) {
        for (RoleEnum enums : RoleEnum.values()) {
            if (enums.roleType == type) {
                return enums;
            }
        }
        return null;
    }

    public static boolean checkRoleList(SysUser user, long... roleTypeArray) {
        if (user == null) {
            throw new SysException("用户为空");
        }
        boolean flag = false;
        for (int i = 0; i < roleTypeArray.length; i++) {
            if (user.getRoleId().intValue() == roleTypeArray[i]) {
                flag = true;
            }
        }
        if (!flag) {
            throw new SysException("用户： " + user.getPhone() + " 角色 " + RoleEnum.getRoleEnum(user.getRoleId().intValue()).roleName + " 无权限访问此接口");
        }
        return true;
    }


}
